package com.telecom.at.json;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.telecom.at.db.dac.Dac;
import com.telecom.at.db.model.AvgFlow;

@WebServlet("/json/JsonScatter")

public class JsonScatter extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // doPost(request, response);
        request.getRequestDispatcher("/WEB-INF/jsp/front/DaysAndSumFlow.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // JSONObject option = new JSONObject();
        // JSONObject tmp1 = new JSONObject();
        List<AvgFlow> flowList = Dac.getInstance().allAvgFlows();
        JSONArray jsonArray = new JSONArray();
        // JSONArray temp = new JSONArray();
        for (AvgFlow avgFlow : flowList) {
            // avgFlow.getDays()
            JSONArray temp = new JSONArray();
            temp.put(avgFlow.getDays());
            temp.put(avgFlow.getSumFlow());
            temp.put(avgFlow.getAvgFlow());

            jsonArray.put(temp);
        }

        JSONArray jsonArray2 = new JSONArray();
        JSONArray temp2 = new JSONArray();
        temp2.put(3);
        temp2.put(3);
        temp2.put(5);
        jsonArray2.put(temp2);
        temp2 = new JSONArray();
        temp2.put(6);
        temp2.put(6);
        temp2.put(8);
        jsonArray2.put(temp2);

        // List<AvgFlow> flowList = Dac.getInstance().allAvgFlows();

        int minAvgFlow = (int) Dac.getInstance().getMinAvgFlow();
        int maxAvgFlow = (int) Dac.getInstance().getMaxAvgFlow();
        // System.out.println(minAvgFlow);
        // System.out.println(flowList);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("minAvgFlow", minAvgFlow);
        jsonObject.put("maxAvgFlow", maxAvgFlow);
        jsonObject.put("jsonArray", jsonArray);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(jsonObject.toString());
    }
}
